/*
 * @Description:
 * @Author: Chetwyn
 * @Date: 2019-06-13 19:42:15
 * @LastEditors: Chetwyn
 * @LastEditTime: 2019-08-30 16:55:24
 */
import { combineReducers } from 'redux';
import { handleActions } from 'redux-actions';
import getApiServer from './ApiServer';

const common = handleActions(
  {
    toggleOverlayContainer: (state, { payload }) => {
      return {
        ...state,
        overlayContainer: payload,
      };
    },
    toggleErrorInfo: (state, { payload }) => {
      return {
        ...state,
        errorInfo: payload,
      };
    },
    toggleLoading: (state, { payload }) => {
      const { visible, successTip } = payload;
      const value = visible ? 1 : -1;
      let loadingCount = state.loadingCount + value;
      loadingCount < 0 && (loadingCount = 0);

      return {
        ...state,
        loadingCount,
        successTip,
      };
    },
    setVerifyCodeCountDown: (state, { payload }) => {
      return {
        ...state,
        verifyCodeCountDown: payload,
      };
    },
    toggleLoginFailureDialog: (state, { payload }) => {
      return {
        ...state,
        loginFailure: payload,
      };
    },
    setTicket: (state, { payload }) => {
      const { ticket } = payload;
      return {
        ...state,
        ticket,
      };
    },
    setInitialProps: (state, { payload }) => {
      const {
        accsys,
        sessionId,
        accessToken,
        deviceId,
        isNightMode = false,
        environment,
        hrName,
        hrId,
        appVersion,
      } = payload;
      const apiServer = getApiServer(environment);
      console.log(`====> setInitialProps:${apiServer}, `, payload);
      return {
        ...state,
        accsys,
        sessionId,
        accessToken,
        deviceId,
        isNightMode,
        apiServer,
        hrName,
        hrId,
        appVersion,
      };
    },
  },
  {
    overlayContainer: {
      visible: false,
      componentCreator: () => {},
    },
    errorInfo: {
      visible: false,
      text: '',
    },
    verifyCodeCountDown: {
      correct: false, // 是否被修正
      active: false,
      remaining: 60,
    },
    loadingCount: 0,
    loginFailure: {
      visible: false,
    },
    ticket: '',
    accsys: '',
    sessionId: '',
    accessToken: '',
    deviceId: '',
    isNightMode: false,
    apiServer: '',
    hrName: '',
    hrId: '',
    appVersion: '',
  }
);

const coreData = handleActions({}, {});

const rootReducer = combineReducers({
  common,
  coreData,
});

export default rootReducer;
